/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.master.modules.doctor.netHosptial.service;


import com.jims.master.common.service.CrudService;
import com.jims.master.modules.doctor.netHosptial.dao.NetOrdersDao;
import com.jims.master.modules.doctor.netHosptial.entity.NetClinicMaster;
import com.jims.master.modules.doctor.netHosptial.entity.NetOrders;
import com.jims.master.modules.doctor.netHosptial.entity.NetTreatRec;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 门诊医嘱记录Service
 * @author qlx
 * @version 2016-09-10
 */
@Service
@Transactional(readOnly = true)
public class NetOrdersService extends CrudService<NetOrdersDao, NetOrders> {

    @Autowired
    private NetClinicMasterService netClinicMasterService;

    @Autowired
    private NetTreatRecService outpTreatRecService;


    //查询最大patient_id
    public String findMaxOutpOrders(){
        return dao.findMaxOutpOrders();
    }

    /**
     * 获得患者未传送的信息
     * @param-patientId
     * @param-visitDate
     * @param-visitNo
     * @return
     */
    public List<NetOrders> getInfoToSend(NetOrders hisOutpOrders){
         return dao.getInfoToSend(hisOutpOrders);
    };

    //改变发送标志
    @Transactional(readOnly = false)
    public Integer send(NetOrders hisOutpOrders){
        return dao.send(hisOutpOrders);
    }

    @Transactional(readOnly = false)
    public void saveAll(NetOrders netOrders){
        NetClinicMaster clinicMaster = netClinicMasterService.get(netOrders.getClinicId());
        if(clinicMaster!=null){
            NetOrders netOrdersSave = new NetOrders();
            if(StringUtils.isEmpty(netOrders.getSerialNo())) {
                netOrdersSave.setPatientId(netOrders.getPatientId());
                netOrdersSave.setVisitDate(clinicMaster.getVisitDate());
                netOrdersSave.setVisitNo(clinicMaster.getVisitNo());
                //serial_no a+num a:医院序号
                String serialNo = "";
                serialNo = findMaxOutpOrders();
                if (StringUtils.isEmpty(serialNo)) {
                    serialNo = "A1";
                } else {
                    serialNo = "A" + serialNo;
                }
                netOrdersSave.setSerialNo(serialNo);
                netOrdersSave.setRemarks("0");
                //ordered_by, doctor,doctor_no
                //todo
               /* Hospital hospital = hospitalService.get(UserUtils.getUser().getOfficeId());
                if(StringUtils.isNotBlank(hospital.getHospitalNum()))
                netOrdersSave.setOrderedBy(hospital.getHospitalNum());*/
                //netOrdersSave.setOrderedBy("110104");
                //todo
                //netOrdersSave.setDoctor(UserUtils.getBaseInfo().getName());
                //netOrdersSave.setDoctorNo(UserUtils.getUser().getRemarks());
                //doctor_noHIS登录名和平台无关
                //ordered_by
                netOrdersSave.setClinicNo(clinicMaster.getClinicNo());
            }else{
                netOrdersSave= dao.getByEntity(netOrders);
            }
            Double netOrdersHeji = Double.valueOf("0");
            String itemNo = "";
            NetTreatRec itemNoEntity = new NetTreatRec();
            itemNoEntity.setVisitDate(netOrders.getVisitDate());
            itemNoEntity.setVisitNo(netOrders.getVisitNo());
            itemNo = outpTreatRecService.findMaxItemNo(itemNoEntity);
            if (StringUtils.isEmpty(itemNo)) {
                itemNo = "1";
            }
            for(NetTreatRec outpTreatRec : netOrders.getList()){
                if(StringUtils.isEmpty(outpTreatRec.getId())) {
                    //VISIT_DATE
                    outpTreatRec.setVisitDate(clinicMaster.getVisitDate());
                    //VISIT_NO
                    outpTreatRec.setVisitNo(clinicMaster.getVisitNo());
                    //SERIAL_NO
                    outpTreatRec.setSerialNo(netOrdersSave.getSerialNo());
                    //ITEM_NO

                    outpTreatRec.setItemNo(String.valueOf(Integer.valueOf(itemNo)+netOrders.getList().indexOf(outpTreatRec)));
                    //ITEM_CLASS
                    outpTreatRec.setItemClass("E");
                    //ITEM_CODE
                    //ITEM_NAME
                    //ITEM_SPEC
                    outpTreatRec.setItemSpec(null);
                    //UNITS
                    outpTreatRec.setUnits(null);
                    //AMOUNT
                    //FREQUENCY
                    outpTreatRec.setFrequency(null);
                    //PERFORMED_BY
                    //COSTS
                    //CHARGES
                    //CHARGE_INDICATOR
                    outpTreatRec.setChargeIndicator("0");
                    //APPOINT_NO
                    outpTreatRec.setAppointNo(netOrdersSave.getSerialNo());
                    //APPOINT_ITEM_NO
                    outpTreatRec.setAppointItemNo(null);
                    //FREQ_DETAIL
                    //WARD_CODE
                    outpTreatRec.setWardCode("");
                    Double heji = mul(Double.valueOf(outpTreatRec.getCosts()), Double.valueOf(outpTreatRec.getAmount()));
                    outpTreatRec.setCosts(new java.text.DecimalFormat("#.00").format(heji));
                    outpTreatRec.setCharges(new java.text.DecimalFormat("#.00").format(heji));
                    outpTreatRecService.save(outpTreatRec);
                }
            }
            netOrdersSave.setCosts(new java.text.DecimalFormat("#.00").format(netOrdersHeji));
            netOrdersSave.setCharges(new java.text.DecimalFormat("#.00").format(netOrdersHeji));
            save(netOrdersSave);
        }

    }
    public Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return new Double(b1.add(b2).doubleValue());
    }
    /**
     * 提供精确的乘法运算。
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static double mul(double v1,double v2){
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     *
     * @param_serialNo
     * @param_visitDate
     * @param_visitNo
     * @return
     */
    public NetOrders getByEntity(NetOrders netOrders){
        return dao.getByEntity(netOrders);
    }

    @Transactional(readOnly = false)
    public void deleteAll(NetOrders netOrders){
       /* netOrders = getByEntity(netOrders);
        OutpTreatRec outpTreatRec = new OutpTreatRec();
        outpTreatRec.setAppointNo(netOrders.getSerialNo());
        OutpTreatRec outpTreatRecSelect = outpTreatRecService.findInfoByAppointNo(outpTreatRec);
        OutpOrdersCosts outpOrdersCosts = new OutpOrdersCosts();
        outpOrdersCosts.setVisitDate(outpTreatRecSelect.getVisitDate());
        outpOrdersCosts.setVisitNo(outpTreatRecSelect.getVisitNo());
        outpOrdersCosts.setSerialNo(outpTreatRecSelect.getVisitNo());
        outpOrdersCostsService.deleteFromAppointNo(outpOrdersCosts);
        outpTreatRecService.deleteFromAppointNo(outpTreatRec);*/
        dao.delete(netOrders);
    }
}